package com.shujia.mr.hw.home5;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;

public class Home5Reducer extends Reducer<Text, Text, NullWritable, Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, NullWritable, Text>.Context context) throws IOException, InterruptedException {

        /**
         *  1. 当Values中只有一个数据时，说明该Key不是主管
         *  2. 当Values中数量不为1 表示有多个员工
         */

        String SJName = "";
        ArrayList<String> emps = new ArrayList<>();
        for (Text value : values) {
            System.out.println(value);
            String oneValue = value.toString();
            if (oneValue.split(",").length == 1) {
                SJName = oneValue;
            } else {
                // 如果长度不为1 是员工的基本信息
                emps.add(oneValue);
            }
        }
        for (String emp : emps) {
            context.write(NullWritable.get(),new Text(emp + "," + SJName));
        }

    }
}
